package com.tj.retrofit_notes;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

//        getGankFuliResult();

        getGankByRxjava();
    }

    /**
     * 获取gank福利
     *
     * get方式
     */
    public void getGankFuliResult(){

        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        //这里的log有4个level， NONE，BASIC，HEADERS，BODY
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new CustomInterceptor())
                .addInterceptor(httpLoggingInterceptor)//打印log
                .connectTimeout(HttpMethod.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(HttpMethod.BASE_URL)//以“/”结尾
                .addConverterFactory(GsonConverterFactory.create())//自动转换json为对象
                .build();

        GankApi gankApi = retrofit.create(GankApi.class);

        Call<GankFuliBean> call =  gankApi.getFuliData(1);
        call.enqueue(new Callback<GankFuliBean>() {
            @Override
            public void onResponse(Call<GankFuliBean> call, Response<GankFuliBean> response) {
                Log.d(TAG, "onResponse: ="+response.body().getResults().get(1).getUrl());
            }

            @Override
            public void onFailure(Call<GankFuliBean> call, Throwable t) {
                Log.d(TAG, "onResponse: onFailure");
            }
        });

    }

    /**
     * Retrofit 集合RXJAVA来处理数据
     *
     * 代码简洁明了
     */
    private void getGankByRxjava(){

        //使用单列
        final GankApi gankApi = HttpMethod.getInstance().getGankApi();

        Observable<GankFuliBean> observable = gankApi.getFuliRxjava(1);

        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())//AndroidSchedulers类需要gradle配置Rxandroid
                .subscribe(new Action1<GankFuliBean>() {
                    @Override
                    public void call(GankFuliBean gankFuliBean) {
                        Log.d(TAG, "call: GankFuliBean" + gankFuliBean.getResults().get(0).getUrl());
                    }
                });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
